package com.lanyan.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.lanyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.mapper.EstateMapper;
import com.lanyan.system.domain.Estate;
import com.lanyan.system.domain.vo.EstateVo;
import com.lanyan.system.service.IEstateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 小区Service业务层处理
 *
 * @author lanyan
 * @date 2024-11-26
 */
@Service
public class EstateServiceImpl extends ServiceImpl<EstateMapper, Estate> implements IEstateService {
    @Autowired
    private EstateMapper estateMapper;

    /**
     * 查询小区
     *
     * @param estateId 小区主键
     * @return 小区
     */
    @Override
    public Estate selectEstateByEstateId(Long estateId) {
        return this.getById(estateId);
    }

    /**
     * 查询小区Vo列表
     *
     * @param estate 小区
     * @return 小区
     */
    @Override
    public List<EstateVo> selectEstateVoList(Estate estate) {
        return selectEstateList(estate)
                .stream()
                .map(x -> (EstateVo) x)
                .collect(Collectors.toList());
    }

    /**
     * 查询小区列表
     *
     * @param estate 小区
     * @return 小区
     */
    @Override
    public List<Estate> selectEstateList(Estate estate) {
        LambdaQueryChainWrapper<Estate> wrapper = this.lambdaQuery().eq(Estate::getDelFlag, "0");
        if (StringUtils.isNotEmpty(estate.getName())) {
            wrapper.like(Estate::getName, estate.getName());
        }
        if (StringUtils.isNotEmpty(estate.getMainImg())) {
            wrapper.eq(Estate::getMainImg, estate.getMainImg());
        }
        if (StringUtils.isNotEmpty(estate.getAddress())) {
            wrapper.eq(Estate::getAddress, estate.getAddress());
        }
        if (StringUtils.isNotEmpty(estate.getLongitude())) {
            wrapper.eq(Estate::getLongitude, estate.getLongitude());
        }
        if (StringUtils.isNotEmpty(estate.getLatitude())) {
            wrapper.eq(Estate::getLatitude, estate.getLatitude());
        }
        return wrapper.list();
    }

    /**
     * 新增小区
     *
     * @param estate 小区
     * @return 结果
     */
    @Override
    public boolean insertEstate(Estate estate) {
        estate.setCreateTime(DateUtils.getNowDate());
        return this.save(estate);
    }

    /**
     * 修改小区
     *
     * @param estate 小区
     * @return 结果
     */
    @Override
    public boolean updateEstate(Estate estate) {
        estate.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(estate);
    }

    /**
     * 批量删除小区
     *
     * @param estateIds 需要删除的小区主键
     * @return 结果
     */
    @Override
    public boolean deleteEstateByEstateIds(Long[] estateIds) {
        return this.lambdaUpdate().set(Estate::getDelFlag, "1").in(Estate::getEstateId, estateIds).update();
    }

    /**
     * 删除小区信息
     *
     * @param estateId 小区主键
     * @return 结果
     */
    @Override
    public boolean deleteEstateByEstateId(Long estateId) {
        return this.lambdaUpdate().set(Estate::getDelFlag, "1").eq(Estate::getEstateId, estateId).update();
    }

    @Override
    public List<Estate> getSort(String name,String latitude, String longitude) {
        return estateMapper.getSort(name,latitude, longitude);
    }
}
